package com.glory.brepda.activity.mfg;


import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.FixtureDataAdapter;
import com.glory.brepda.adapter.ItmeDataSortAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.BomEntity;
import com.glory.brepda.entity.EdcEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;


import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

public class FixtureMaintainActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.rvFixtureList)
    RecyclerView rvFixtureList;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.flStepList)
    FrameLayout flStepList;
    @BindView(R.id.tvStepName)
    TextView tvStepName;
    @BindView(R.id.ivStep)
    ImageView ivStep;
    @BindView(R.id.tvPartName)
    TextView tvPartName;
    @BindView(R.id.etToolID)
    FreeUI_ClearEditText etToolID;
    @BindView(R.id.tvToolName)
    TextView tvToolName;
    @BindView(R.id.tvToolSpec)
    TextView tvToolSpec;
    @BindView(R.id.etChipLOTID)
    FreeUI_ClearEditText etChipLOTID;
    @BindView(R.id.tvChipName)
    TextView tvChipName;
    @BindView(R.id.etToollife)
    FreeUI_ClearEditText etToollife;
    @BindView(R.id.tvBCOPType)
    TextView tvBCOPType;
    @BindView(R.id.ivBCOPArrow)
    ImageView ivBCOPArrow;
    @BindView(R.id.flBCOPList)
    FrameLayout flBCOPList;
    @BindView(R.id.LLak)
    LinearLayout LLak;

    private FixtureDataAdapter mFixtureDataAdapter;
    private ArrayList<String> stepList = new ArrayList<>();
    private FreeUI_GeneralPop mStepPop,mStepPop2;
    private ArrayList<String> zj = new ArrayList<>(Arrays.asList("Z1轴","Z2轴"));
    private String zd = "无";  //站点
//    private  String stepName;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_fixturemaintain;
    }

    @Override
    protected void initView() {
        tvTitle.setText("治具BOM维护");
        setMainLayout(llMain);

        mFixtureDataAdapter = new FixtureDataAdapter(null);
        mFixtureDataAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mFixtureDataAdapter.isFirstOnly(true);
        initRecyclerView(rvFixtureList, this);
        rvFixtureList.setAdapter(mFixtureDataAdapter);
        requesInfo("QUERYSTEPINFO");

        flBCOPList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mStepPop2 != null) {
                    if (mStepPop2.isShowing()) {
                        mStepPop2.dismiss();
                    }
                } else {
                    mStepPop2 = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flBCOPList.getWidth(), zj, ivBCOPArrow, (ty, i) -> {
                        tvBCOPType.setText(ty);
                        zd = ty;
                        Toast.makeText(getApplicationContext(),ty,Toast.LENGTH_SHORT).show();
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivBCOPArrow);
                mStepPop2.showAsDropDown(flBCOPList, 0, 0);
            }
        });
    }

    @Override
    protected void initData() {
        initEdit(etLot,s ->{
            requesInfo("QUERYLOTINFO");
        });
        initEdit(etToolID,s ->{
            requesInfo("QUERYTOOLINFO");
        });
        initEdit(etChipLOTID,s ->{
            requesInfo("QUERYMLOTINFO");
        });
    }

    @OnClick({R.id.tvBaseSubmit,R.id.flStepList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (tvStepName.getText().toString().equals("")){
                    showToast("工步不能为空",false);
                    return;
                }
                if (getText(etLot).equals("")){
                    showToast("请扫描随工单批号",false);
                    return;
                }
                if (getText(etToolID).equals("")){
                    showToast("请扫描治具批号",false);
                    return;
                }
                SaveBom();
                break;
            case R.id.flStepList:
                if (mStepPop != null) {
                    if (mStepPop.isShowing()) {
                        mStepPop.dismiss();
                    }
                } else {
                    mStepPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flStepList.getWidth(), stepList, ivStep, (codeS, i) -> {
//                        stepName=codeS;
                        tvStepName.setText(codeS);
                        if(codeS.contains("划片工序")){
                            LLak.setVisibility(View.GONE);
                        }else{
                            LLak.setVisibility(View.VISIBLE);
                        }
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivStep);
                mStepPop.showAsDropDown(flStepList, 0, 0);
                break;
        }
    }

    private void requesInfo(String actiontype) {
        FreeUI_EntityCallBack<BaseResponseBean<BomEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<BomEntity>>
                (new TypeToken<BaseResponseBean<BomEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<BomEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    BomEntity bomEntity = response.body().getResponse().getBody();
                    switch (actiontype){
                        case "QUERYSTEPINFO":
                            for (BomEntity.StepEntity step:bomEntity.getSTEPLIST()){
                                stepList.add(step.getNAME()+"/"+step.getDESCRIPTION());
                            }
                            break;
                        case "QUERYLOTINFO":
                            tvPartName.setText(bomEntity.getPARTNAME());
                            break;
                        case "QUERYTOOLINFO":
                            tvToolName.setText(bomEntity.getTOOLNAME());
                            tvToolSpec.setText(bomEntity.getTOOLSPEC());
                            break;
                        case "QUERYMLOTINFO":
                            tvChipName.setText(bomEntity.getCHIPNAME());
                            break;

                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BomEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"TOOLBOMBASIC");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE",actiontype);
        switch (actiontype){
            case "QUERYLOTINFO":
                map.addProperty("LOTID",getText(etLot));
                break;
            case "QUERYTOOLINFO":
                map.addProperty("TOOLID",getText(etToolID));
                break;
            case "QUERYMLOTINFO":
                map.addProperty("CHIPLOTID",getText(etChipLOTID));
                break;
        }
        NetUtils.requestPostNet("IF.TOOLBOMBASIC","Common", FixtureMaintainActivity.this,map,callBack);
    }

    private void SaveBom() {

//        判断选择工步划片，必填治具批号，芯片批号
//        if(tvStepName.getText().toString().contains("划片")){
//            if(getText(etToolID).equals("") || getText(etChipLOTID).equals("")){
//                showToast("划片工序需选择治具批号，芯片批号",false);
//                return;
//            }
//        }
//        治具名为顶针，吸嘴时，必填芯片批号
//        if(tvToolName.getText().toString().equalsIgnoreCase("Thimble") || tvToolName.getText().toString().equalsIgnoreCase("SuctionNozzle")){
//            if(getText(etChipLOTID).equals("") || getText(etChipLOTID) == null){
//                showToast("治具名称为：顶针，吸嘴时。芯片批号必填！！！",false);
//                return;
//            }
//        }

        FreeUI_EntityCallBack<BaseResponseBean<BomEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<BomEntity>>
                (new TypeToken<BaseResponseBean<BomEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<BomEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    mFixtureDataAdapter.setNewData(response.body().getResponse().getBody().getTOOLBOMLINELIST());
                    etToolID.setText("");
                    tvToolName.setText("");
                    tvToolSpec.setText("");
                    etChipLOTID.setText("");
                    tvChipName.setText("");
                    etToollife.setText("");
                    checkbox.setChecked(false);
                    etToolID.requestFocus();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<BomEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"TOOLBOMBASIC");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","SAVEBOM");
        map.addProperty("STEPNAME",tvStepName.getText().toString().split("/")[0]);
        map.addProperty("LOTID",getText(etLot));
        map.addProperty("PARTNAME",tvPartName.getText().toString());
        map.addProperty("TOOLID",getText(etToolID));
        map.addProperty("TOOLNAME",tvToolName.getText().toString());
        map.addProperty("TOOLSPEC",tvToolSpec.getText().toString());
        map.addProperty("CHIPLOTID",getText(etChipLOTID));
        map.addProperty("CHIPNAME",tvChipName.getText().toString());
        map.addProperty("TOOLLIFE",getText(etToollife));
        map.addProperty("RESERVED2",zd);
        map.addProperty("ISALTERNATE",checkbox.isChecked()?"Y":"N");
        NetUtils.requestPostNet("IF.TOOLBOMBASIC","Common", FixtureMaintainActivity.this,map,callBack);
    }

}